package client.gui;

import shared.PlayerPortfolio;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import client.ClientController;

/**
 * Gui for a game state<br/>
 * Will be replaced by Ryan's GUI code
 * @author Joel Garboden
 */
public class ClientGUI_Game extends JFrame
{
  private PlayerPortfolio portfolio;
  public boolean gameStarted;

  public JButton finishTurn;
  private JButton quit;

  private ChatComponent chatComponent;

  private ClientController controller;

  /**
   * Constructor
   * @param controller reference to gui owner
   */
  public ClientGUI_Game(ClientController controller, int gameID)
  {
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    gameStarted = false;
    this.setTitle("InEquity - Game " + gameID);

    this.controller = controller;

    //portfolio = new PlayerPortfolio(controller.playerName);

    chatComponent = new ChatComponent(controller, new Dimension(100, 200));

    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(640, 480));
    setMinimumSize(new Dimension(640, 480));
    finishTurn = new JButton("Start Game");
    finishTurn.setEnabled(false);

    Box optionsBox = Box.createHorizontalBox();
    optionsBox.add(finishTurn);

    quit = new JButton("Quit");

    optionsBox.add(quit);

    add(optionsBox, BorderLayout.EAST);

    add(chatComponent, BorderLayout.SOUTH);

    chatComponent.enableChat(true);

    setLocationRelativeTo(null);

    finishTurn.addActionListener(new java.awt.event.ActionListener()
    {
      @Override
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        finishTurn();
      }
    });

    quit.addActionListener(new java.awt.event.ActionListener()
    {
      @Override
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        quit();
      }
    });
  }

  /**
   * Queries server for quit authorization
   */
  public void quit()
  {
    controller.quit();
  }

  /**
   * Sends a request to pass the turn
   */
  public void finishTurn()
  {
    controller.finishTurn();
  }

  public void gameStarted(PlayerPortfolio portfolio)
  {
    this.portfolio = portfolio;
    gameStarted = true;
    this.finishTurn.setText("Finish turn");
  }

  /**
   * Mutator
   * @param portfolio new portfolio to be set
   */
  public void setPortfolio(PlayerPortfolio portfolio)
  {
    this.portfolio = portfolio;
  }

  /**
   * Accessor
   * @return current portfolio
   */
  public PlayerPortfolio getPortfolio()
  {
    return portfolio;
  }

  /**
   * Toggles turn
   * @param turnToggle turn state
   */
  public void toggleTurn(boolean turnToggle)
  {
    finishTurn.setEnabled(turnToggle);
  }

  /**
   * Posts chat to gui
   * @param name senders name
   * @param chatMsg incoming chat message
   */
  public void postChat(String name, String chatMsg)
  {
    chatComponent.postChat(name, chatMsg);
  }

  /**
   * Mutator
   * @param toggle chat active state
   */
  public void toggleChat(boolean toggle)
  {
    chatComponent.enableChat(toggle);
  }

  public void updateData()
  {
    System.out.println("------------------Updated GUI----------------------");
    portfolio.printAll();  
  }

}
